package com.jinwei.qunguanjia.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.jinwei.qunguanjia.common.constant.UserConstants;
import com.jinwei.qunguanjia.common.core.domain.model.Condition;
import com.jinwei.qunguanjia.common.core.page.PageDataInfo;
import com.jinwei.qunguanjia.common.utils.*;
import com.jinwei.qunguanjia.user.dao.SensitiveWordDAO;
import com.jinwei.qunguanjia.user.dto.SensitiveWordDTO;
import com.jinwei.qunguanjia.user.po.SensitiveWordPO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("sensitiveWordService")
public class SensitiveWordServiceImpl implements SensitiveWordService {

    @Resource
    private SensitiveWordDAO sensitiveWordDAO;

    @Override
    public String save(SensitiveWordDTO sensitiveWordDTO) {
        SensitiveWordPO sensitiveWordPO = new SensitiveWordPO();
        BeanUtils.copyProperties(sensitiveWordDTO, sensitiveWordPO);
        String sensitiveWordId = IdUtils.getWorkerId();
        sensitiveWordPO.setSensitiveWordId(sensitiveWordId);
        sensitiveWordPO.setCreateTime(DateUtils.getDateTime());
        sensitiveWordDAO.insert(sensitiveWordPO);
        return sensitiveWordId;
    }

    @Override
    public void remove(String id) {
        sensitiveWordDAO.deleteById(id);
    }

    @Override
    public void update(SensitiveWordDTO sensitiveWordDTO) {
        SensitiveWordPO sensitiveWordPO = new SensitiveWordPO();
        BeanUtils.copyProperties(sensitiveWordDTO, sensitiveWordPO);
        sensitiveWordDAO.updateById(sensitiveWordPO);
    }

    @Override
    public SensitiveWordDTO get(String id) {
        SensitiveWordPO sensitiveWordPO = sensitiveWordDAO.selectById(id);
        if (null == sensitiveWordPO) {
            return null;
        }
        SensitiveWordDTO sensitiveWordDTO = new SensitiveWordDTO();
        BeanUtils.copyProperties(sensitiveWordPO, sensitiveWordDTO);
        return sensitiveWordDTO;
    }

    @Override
    public SensitiveWordDTO get(SensitiveWordDTO sensitiveWordDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(sensitiveWordDTO);
        // 拼接查询条件
        QueryWrapper<SensitiveWordPO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        SensitiveWordPO sensitiveWordPO = sensitiveWordDAO.selectOne(queryWrapper);
        if (null == sensitiveWordPO) {
            return null;
        }
        BeanUtils.copyProperties(sensitiveWordPO, sensitiveWordDTO);
        return sensitiveWordDTO;
    }

    @Override
    public Long count(SensitiveWordDTO sensitiveWordDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(sensitiveWordDTO);
        // 拼接查询条件
        QueryWrapper<SensitiveWordPO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        return sensitiveWordDAO.selectCount(queryWrapper);
    }

    @Override
    public List<SensitiveWordDTO> list(SensitiveWordDTO sensitiveWordDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(sensitiveWordDTO);
        // 拼接查询条件
        QueryWrapper<SensitiveWordPO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        return JsonUtils.copyList(sensitiveWordDAO.selectList(queryWrapper), SensitiveWordDTO.class);
    }

    @Override
    public PageDataInfo<SensitiveWordDTO> listPage(SensitiveWordDTO sensitiveWordDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(sensitiveWordDTO);
        // 拼接查询条件
        QueryWrapper<SensitiveWordPO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        List<SensitiveWordPO> list = sensitiveWordDAO.selectList(queryWrapper);
        PageInfo<SensitiveWordPO> pageInfo = new PageInfo<>(list);
        PageDataInfo<SensitiveWordDTO> pageDataInfo = new PageDataInfo<>();
        pageDataInfo.setData(JsonUtils.copyList(list, SensitiveWordDTO.class));
        pageDataInfo.setTotal(pageInfo.getTotal());
        pageDataInfo.setTotalPage(PageCountUtils.getPageCount(pageInfo.getTotal(), pageInfo.getPageSize()));
        return pageDataInfo;
    }

    @Override
    public String checkUnique(SensitiveWordDTO sensitiveWordDTO) throws IllegalAccessException {
        String id = StringUtils.isNull(sensitiveWordDTO.getSensitiveWordId()) ? "-1" : sensitiveWordDTO.getSensitiveWordId();
        SensitiveWordDTO info = get(sensitiveWordDTO);
        if (StringUtils.isNotNull(info) && !info.getSensitiveWordId().equals(id)) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
    
}
